#!/usr/bin/env python
#coding:gbk

CLS_VERSION = "1.0"
import os, shutil
import argparse

def onerror(func, path, exc_info):
    import stat
    if not os.access(path, os.W_OK):
        # Is the error an access error ?
        os.chmod(path, stat.S_IWUSR)
        func(path)
    else:
        raise


def help():
    print """Usage:
    python.exe clearsvn.py dirpath
    """

def process(folder):
    for root, dirs, files in os.walk(folder):
        for dir in dirs:
            if dir == ".svn" or dir == "_svn":
                destdir = os.path.join(root, dir)
                print "Remove: %s" % destdir
                shutil.rmtree(destdir, onerror = onerror)


def main():
    parser = argparse.ArgumentParser(description = "Clear '.svn' and '_svn' folders")
    parser.add_argument('dirs', metavar = 'dirpath', default='.', nargs = '*', help = 'root of clear action, default is %(default)s')
    parser.add_argument('-v', '--version', action='version', version = "%%(prog)s %s" % CLS_VERSION)
    args = parser.parse_args()
    for folder in args.dirs:
        process(folder)

if __name__ == "__main__":
    main()
